#coding:UTF8
'''
Created on 2018年8月13日

@author: Administrator
'''
# import networkx as nx
# from visibility_graph import visibility_graph
import matplotlib.pyplot as plt
import networkx as nx
import networkx.algorithms.community as com 
import itertools
import numpy as np
import math

def run():
    series = [21.36, 31.18, 36.91, 37.12, 44.04, 53.22, 60.23, 69.53, 76.77, 91.64, 65.58, 89.3, 98.18, 125.61, 124.84, 132.18, 136.79, 143.19, 165.35, 152.71, 165.68, 181.4, 204.26, 272.53, 244.71, 385.45, 444.54, 464.63, 520.1, 526.47, 533.39, 733.72, 668.61, 651.23, 614.09, 639.6, 653.59, 690.43, 719.33]
    series = [1263.6, 1278.6, 1414.9, 1785, 1963.8, 1973.38, 2158.89, 2206.96, 2357.33, 2443.74, 3163.62, 3877.06, 10208.5, 12201.9, 13597.78, 14622.02, 13777.2, 13645.13, 14941, 16321.98];
#     G = visibility_graph( series )
    G = create(series) 
    
#     G = nx.karate_club_graph()
    comp = com.centrality.girvan_newman(G)
#     print tuple(sorted(c) for c in next(comp))
    k = 6
    limited = itertools.takewhile(lambda c: len(c) <= k, comp)
    
    for communities in limited:
        c = (tuple(sorted(c) for c in communities))
        print c 
        print com.modularity(G, c)
#     for lm in limited:
#         print com.modularity(G, lm)  
        
#     return
    #     G=nx.dodecahedral_graph()
#     nx.draw(G, pos = nx.random_layout(G), node_color = 'b', edge_color = 'r', with_labels = True, font_size =18, node_size =20)  # networkx draw()
#     plt.draw()  # pyplot draw()
    
#     c = list(greedy_modularity_communities(G))
    
#     return
#     nx.draw(G, with_labels = True)
#     plt.show()
    
    degree(G)
#     output(G)
    
def degree(G):
    degree = nx.degree_histogram(G)#返回图中所有节点的度分布序列
    x = range(len(degree))#生成X轴序列，从1到最大度
    y = [z / float(sum(degree))for z in degree]#将频次转化为频率，利用列表内涵
    print degree 
    print x
    print y
    
    lnx = [math.log(f, math.e) for f in x[1:]]
    lny = [math.log(f, math.e) for f in y[1:]]
    
    
#     plt.loglog(x, y, color="blue", linewidth=2)#在双对坐标轴上绘制度分布曲线
#     plt.loglog(x, y, color="blue", linewidth=2)#在双对坐标轴上绘制度分布曲线
#     plt.semilogy(x, y, color="blue", linewidth=2)#在双对坐标轴上绘制度分布曲线
#     plt.show()#显示图表
    
    plt.semilogy(x[1:], y[1:], color="blue", linewidth=2)#在双对坐标轴上绘制度分布曲线
#     plt.semilogy(x[1:], lny, color="blue", linewidth=2)#在双对坐标轴上绘制度分布曲线
    plt.show()#显示图表
    
    print '*' * 20
    print x[1:], lny
    
    ps = np.polyfit(x[1:], lny, 1)
    print ps
    print np.poly1d(ps)
#     p = polyfit(log(x1),log(y1),1) 
    
def output(G):
    edges = G.edges()
    print 'source, target'
    for c in edges: print str(c[0]) + ', ' + str(c[1]) 
        


def create(ts):
    edges = []
    i, j, second = 0, 1, None
    while i < (len(ts) - 1):
        if j==1: second = ts[i + j]
        
        '''遇到更高点， i前进'''
        if ts[i + j] >= ts[i]:
            edges.append((i, i + j))
            i, j = i + 1, 1
        else:
            '''可视检测'''
            if ts[i + j] >= second: 
                second = ts[i + j]
                edges.append((i, i + j))
            j += 1
            '''虽未遇到高点，但已末尾'''
            if i + j == (len(ts)): i, j = i + 1, 1
        '''End else'''
    '''End while'''
    G = nx.Graph()
    G.add_edges_from(edges)
    return G 
# [(e[0], e[1]) for e in edges]
'''End Create'''    
       
def test():
    ts = [0.71, 0.53, 0.58, 0.29, 0.30, 0.77, 0.01, 0.76, 0.81, 0.71, 0.05, 0.41, 0.86, 0.79, 0.37, 0.96, 0.87, 0.06, 0.95, 0.36]  
    G = create(ts)
#     G = nx.Graph()
#     G.add_edges_from(net)
    nx.draw(G, with_labels = True)
    plt.show()
#     print net
    print G.edges()
           
if __name__ == '__main__':  
    run()

#     print len(G.nodes())
#     print G.edges()
#     print G.node[1]
    
    pass
